package View;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import Actor.Actor;
import Main.Constant;
import Map.Block;
import Map.EnumTile;
import Util.CoordinateUtil;

public class MapView {
	ViewManager viewManager;
	BufferedImage img;
	public MapView(ViewManager _viewManager) {
		viewManager = _viewManager;
		img = null;
		System.out.println(System.getProperty("java.class.path"));
		try {
		    img = ImageIO.read(new File(System.getProperty("java.class.path")+"/1.png"));
		    System.out.println(System.getProperty("java.class.path"));
		} catch (IOException e) {
		}
	}
	
	public void paintComponent(Graphics g) {

		Color color = g.getColor();
		g.setColor(Color.BLACK);

		int leftUpX = 0;
		int leftUpY = 0;
		int leftDownX = 0;
		int leftDownY = 0;
		int rightUpX = 0;
		int rightUpY = 0;
		int rightDownX = 0;
		int rightDownY = 0;
		
		DrawObject map = viewManager.getTile();

		 leftUpX = 0;
		 leftUpY = 0;
		for (int i = 0; i < Constant.VIEWSIZE; i++) {
			for (int j = 0; j < Constant.VIEWSIZE; j++) {

				leftDownX = leftUpX;
				leftDownY = leftUpY + Constant.tileHeight;
				rightUpX = leftUpX + Constant.tileWidth;
				rightUpY = leftUpY;
				rightDownX = rightUpX;
				rightDownY = leftDownY;
				int[] pointx = CoordinateUtil.isoXTo2DX(new int[] { leftUpX,
						leftDownX, rightDownX, rightUpX, leftUpX }, new int[] {
						leftUpY, leftDownY, rightDownY, rightUpY, leftUpY });
				int[] pointy = CoordinateUtil.isoYTo2DY(new int[] { leftUpX,
						leftDownX, rightDownX, rightUpX, leftUpX }, new int[] {
						leftUpY, leftDownY, rightDownY, rightUpY, leftUpY });
				EnumTile tile = map.tile[i][j].getType();
				for (int point = 0; point < pointy.length; point++) {
					pointy[point] = pointy[point] - map.tile[i][j].z;
				}
				if (tile == EnumTile.NORMAL) {

					color = g.getColor();
					for (int k = 0; k < pointx.length; k++)
						drawPoint(g, pointx[k], pointy[k]);
					//g.fillPolygon(pointx, pointy, pointx.length);
					g.setColor(color);
					

				} else if (tile == EnumTile.ACTOR) {
					for (int k = 0; k < pointx.length; k++)
						drawPoint(g, pointx[k], pointy[k]);
					g.fillPolygon(pointx, pointy, pointx.length);
				} 
				else if (tile == EnumTile.BULLET) {
					color = g.getColor();
					g.setColor(Color.RED);
					for (int k = 0; k < pointx.length; k++)
						drawPoint(g, pointx[k], pointy[k]);
					g.fillPolygon(pointx, pointy, pointx.length);
					g.setColor(color);
				} else {
					for (int k = 0; k < pointx.length; k++)
						drawPoint(g, pointx[k], pointy[k]);
					//g.fillPolygon(pointx, pointy, pointx.length);
					// g.drawPolygon(pointx, pointy, pointx.length);
					//if(i%2==0&&j%2==0)
						//g.drawImage(img, pointx[0]-20, pointy[0], null);
				}

				leftUpX += Constant.tileWidth;
			}
			leftUpY += Constant.tileHeight;
			leftUpX = 0;
		}
		
		
		
		
		
		
		realDraw(g, map);

		

	}

	private void realDraw(Graphics g, DrawObject map) {
		Color color;
		int leftUpX;
		int leftUpY;
		int leftDownX;
		int leftDownY;
		int rightUpX;
		int rightUpY;
		int rightDownX;
		int rightDownY;
		for(Block block:map.continent.getBlocks()){
			
			color = g.getColor();
			g.setColor(Color.blue);
			leftUpX = block.x*Constant.tileHeight-viewManager.getTestx();
			leftUpY =  block.y*Constant.tileHeight-viewManager.getTesty();
			leftDownX = leftUpX;
			leftDownY = leftUpY + Constant.tileHeight*block.height;
			rightUpX = leftUpX + Constant.tileWidth*block.width;
			rightUpY = leftUpY;
			rightDownX = rightUpX;
			rightDownY = leftDownY;
			int[] pointx = CoordinateUtil.isoXTo2DX(new int[] { leftUpX,
					leftDownX, rightDownX, rightUpX, leftUpX }, new int[] {
					leftUpY, leftDownY, rightDownY, rightUpY, leftUpY });
			int[] pointy = CoordinateUtil.isoYTo2DY(new int[] { leftUpX,
					leftDownX, rightDownX, rightUpX, leftUpX }, new int[] {
					leftUpY, leftDownY, rightDownY, rightUpY, leftUpY });
			g.fillPolygon(pointx, pointy, pointx.length);
			g.setColor(color);
		}

		

		
		
		
		
		for(Actor monster:map.monster){
			
			leftUpX = monster.getX()*10 - viewManager.getTestx();
			leftUpY = monster.getY()*10 - viewManager.getTesty();
			//System.out.println(leftUpX+","+leftUpY);
			leftDownX = leftUpX;
			leftDownY = leftUpY + Constant.tileHeight;
			rightUpX = leftUpX + Constant.tileWidth;
			rightUpY = leftUpY;
			rightDownX = rightUpX;
			rightDownY = leftDownY;
			int[] pointx = CoordinateUtil.isoXTo2DX(new int[] { leftUpX,
					leftDownX, rightDownX, rightUpX, leftUpX }, new int[] {
					leftUpY, leftDownY, rightDownY, rightUpY, leftUpY });
			int[] pointy = CoordinateUtil.isoYTo2DY(new int[] { leftUpX,
					leftDownX, rightDownX, rightUpX, leftUpX }, new int[] {
					leftUpY, leftDownY, rightDownY, rightUpY, leftUpY });
			color = g.getColor();
			g.setColor(Color.BLACK);
			g.fillPolygon(pointx, pointy, pointx.length);
			g.setColor(color);
		}	
		

		
		Actor actor = map.actors.get(0);
		leftUpX = actor.getX() - viewManager.getTestx();
		leftUpY = actor.getY() - viewManager.getTesty();
		leftDownX = leftUpX;
		leftDownY = leftUpY + Constant.tileHeight;
		rightUpX = leftUpX + Constant.tileWidth;
		rightUpY = leftUpY;
		rightDownX = rightUpX;
		rightDownY = leftDownY;
		int[] pointx1 = CoordinateUtil.isoXTo2DX(new int[] { leftUpX,
				leftDownX, rightDownX, rightUpX, leftUpX }, new int[] {
				leftUpY, leftDownY, rightDownY, rightUpY, leftUpY });
		int[] pointy1 = CoordinateUtil.isoYTo2DY(new int[] { leftUpX,
				leftDownX, rightDownX, rightUpX, leftUpX }, new int[] {
				leftUpY, leftDownY, rightDownY, rightUpY, leftUpY });
		color = g.getColor();
		g.setColor(Color.GREEN);
		g.fillPolygon(pointx1, pointy1, pointx1.length);
		g.setColor(color);
		
		
		
		for(List<Point> path:viewManager.bullets1.keySet()){

			int _targetx = path.get(0).x*10-viewManager.getTestx();
			int _targety = path.get(0).y*10-viewManager.getTesty();
			int x = CoordinateUtil.isoXTo2DX( _targetx,_targety) + Constant.frameHeight;
			int y = CoordinateUtil.isoYTo2DY(_targetx,_targety);
			
			int targetx = path.get(path.size()-1).x*10-viewManager.getTestx();
			int targety = path.get(path.size()-1).y*10-viewManager.getTesty();
			int _x = CoordinateUtil.isoXTo2DX(targetx,targety) + Constant.frameHeight;
			int _y = CoordinateUtil.isoYTo2DY(targetx,targety);
			color = g.getColor();
			g.setColor(Color.BLACK);
			g.drawLine(x, y, _x, _y);
			g.setColor(color);
		}	
		for(List<Point> path:viewManager.bullets1.keySet()){
			int a = viewManager.bullets1.get(path);

			int targetx = path.get(a).x*10-viewManager.getTestx();
			int targety = path.get(a).y*10-viewManager.getTesty();
			int _x = CoordinateUtil.isoXTo2DX(targetx,targety) + Constant.frameHeight;
			int _y = CoordinateUtil.isoYTo2DY(targetx,targety);
			color = g.getColor();
			g.setColor(Color.BLACK);
			g.drawString("S", _x, _y);
			g.setColor(color);
		}	


	}
	private void drawPoint(Graphics g, int pointx, int pointy) {
		//g.drawLine(pointx, pointy, pointx, pointy);
	}

}
